// pages/compony-pages/compony-select-skill/compony-select-skill.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        skillArr: [],
        selectSkills: [],
        showDialog: false,
        customSkill: ''
    },
    //保存
    clickSave(){
        let skillNameArr = [];
        let selectSkills = this.data.selectSkills;
        selectSkills.forEach(function(item){
            skillNameArr.push(item.SkillName);
        });
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];  //上一个页面
        wx.navigateBack({
            delta: 1,
            success: function (res) {
                prevPage.setData({
                    skillNameArr: skillNameArr,
                    skills: selectSkills
                });
            }
        });      
    },
    //找不到符合的标签
    clickNotLabel(){
        this.setData({
            showDialog:true
        });
    },
    // 对话框事件
    dialogInput(e) {
        this.setData({
            customSkill: e.detail.value
        })
    },
    cancel() {
        this.setData({
            showDialog: false
        })
    },
    confirm() {
        if (this.data.customSkill) {
            let obj = {
                PositionId: "",
                SkillName: this.data.customSkill,
                SortNo: this.data.skillArr.length
            }
            console.log(obj);
            let newSkillArr = this.data.skillArr;
            newSkillArr.push(obj);
            this.setData({
                skillArr: newSkillArr,
                showDialog:false
            });
        }
    },
    // 已选标签删除点击事件
    clickItemSkill(e) {
        let index = e.currentTarget.dataset.index;
        let selectSkills = this.data.selectSkills;
        let skillArr = this.data.skillArr;
        let skillArrIndex = 0;
        for (let i = 0; i < skillArr.length; i++) {
            if (selectSkills[index].SkillName === skillArr[i].SkillName) {
                skillArrIndex = i;
                break;
            }
        }
        let key = `skillArr[${skillArrIndex}].isActive`;
        selectSkills.splice(index, 1);
        this.setData({
            selectSkills: selectSkills,
            [key]: false
        })
    },
    // 标签选择点击事件
    selecCategorytItem(e) {
        let index = e.currentTarget.dataset.index;
        let skillArr = this.data.skillArr;
        let selectSkills = this.data.selectSkills;
        if (this.data.selectSkills.length >= 3 && !skillArr[index].isActive) {
            return;
        }
        if (skillArr[index].isActive) {
            for (let i = 0; i < selectSkills.length; i++) {
                if (selectSkills[i].SkillName === skillArr[index].SkillName) {
                    selectSkills.splice(i, 1);
                    break;
                }
            }
        } else {
            selectSkills.push(skillArr[index]);
        }
        let key = `skillArr[${index}].isActive`;
        this.setData({
            selectSkills: selectSkills,
            [key]: !skillArr[index].isActive
        })
    },
    /**
     * 网络请求
     */
    getPositionSkillData(id) {
        let that = this;
        wx.showLoading({
            title: '加载中...',
            mask: true
        })
        wx.request({
            url: app.globalData.serverAddress.getPositionSkillData,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            data: {
                positionId: id
            },
            method: 'GET',
            success: function(res) {
                console.log("getPositionSkillData success：", res.data.BodyContent);
                if (res.statusCode == 200 && res.data.CodeStatus == 20000) {
                    that.setData({
                        skillArr: res.data.BodyContent
                    });
                }
            },
            fail: function(res) {
                console.log("getPositionSkillData fail", res)
            },
            complete: function(res) {
                wx.hideLoading()
            },
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getPositionSkillData(options.id);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})